package com.itheima.mapper;

import com.github.pagehelper.Page;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CheckGoupMapper {


    /**
     *
     * @param checkGroup
     */
    void add(CheckGroup checkGroup);

    /**
     *
     * @param id
     */
    @Insert("insert into t_checkgroup_checkitem(checkgroup_id,checkitem_id) values(#{checkGroupId},#{checkItemId})")
    void setCheckGroupAndCheckItem(Integer checkGroupId, Integer checkItemId);

    /**
     * 删除检查组
     * @param id
     */
    @Delete("delete from t_checkgroup where id = #{id}")
    void deleteById(Integer id);

    /**
     * 根据检查组id删除中间表数据（清理原有关联关系）
     * @param id
     */
    @Delete("delete from t_checkgroup_checkitem where checkgroup_id = #{id}")
    void deleteCheckGroupAndCheckItem(Integer id);


    /**
     * 根据检查组id查询检查项id
     * @param checkGroupId
     * @return
     */
    @Select("SELECT `t_checkgroup_checkitem`.`checkitem_id` AS id " +
            "FROM `t_checkgroup_checkitem` " +
            "LEFT JOIN `t_setmeal_checkgroup`" +
            "ON `t_checkgroup_checkitem`.`checkgroup_id` = `t_setmeal_checkgroup`.`checkgroup_id`" +
            "WHERE `t_setmeal_checkgroup`.`setmeal_id` =#{checkGroupId} ")
    List<Integer>  findCheckGroupIdsBySetmealId(Integer checkGroupId);



    /**
     * 根据检查组id查询检查组
     * @param id
     * @return
     */
    Page<CheckItem> pageQuery(String queryString);

    /**
     * 根据检查组id查询检查组
     * @param id
     * @return
     */
    @Select("select * from t_checkgroup where id = #{id}")
    CheckGroup findById(Integer id);

    /**
     * 根据检查组id查询检查项
     * @param id
     * @return
     */
    @Select("select * from t_checkitem where id in (select checkitem_id from t_checkgroup_checkitem where checkgroup_id = #{id})")
    List<CheckItem> findCheckItemByCheckGroupId(Integer id);

//    @Select("select checkitem_id from t_checkgroup_checkitem where checkgroup_id = #{id}")
//    List<CheckItem> findCheckItemByCheckGroupId(Integer id);

    /**
     * 查询所有检查组
     * @return
     */
    @Select("select * from t_checkgroup")
    List<CheckGroup> findAll();

    /**
     * 编辑检查组
     * @param checkGroup
     */
    @Update("update t_checkgroup set code = #{code},name = #{name},helpCode = #{helpCode},sex = #{sex},remark = #{remark},attention = #{attention} where id = #{id}")
    void edit(CheckGroup checkGroup);



}
